﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.DataAccess;
using System.Collections.ObjectModel;

namespace University.Workshop.WinApp
{
    public partial class ListEmail : Form
    {
        private SendMail send = new SendMail();
        public ListEmail()
        {
            InitializeComponent();
            send.SelectAdministrator(listAdministrator);
            send.SelectClient(listClient);
            send.SelectEmployee(listEmployee);
        }

        private void listAdministrator_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (e.Item.Checked)
            {
                ListViewItem item = new ListViewItem(e.Item.SubItems[1].Text);
                item.SubItems.Add(e.Item.SubItems[2].Text);
                listMailSend.Items.Add(item);
            }
            else
            {
                foreach (ListViewItem item in listMailSend.Items)
                {
                    if (e.Item.SubItems[1].Text == item.SubItems[0].Text)
                    {
                        listMailSend.Items[item.Index].Remove();
                    }
                }
            }
        }
        private void bSelectAdministrator_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listAdministrator.Items)
            {
                item.Checked = true;
            }
        }

        private void bClient_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listClient.Items)
            {
                item.Checked = true;
            }
        }

        private void listClient_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (e.Item.Checked)
            {
                ListViewItem item = new ListViewItem(e.Item.SubItems[1].Text);
                item.SubItems.Add(e.Item.SubItems[2].Text);
                listMailSend.Items.Add(item);
            }
            else
            {
                foreach (ListViewItem item in listMailSend.Items)
                {
                    if (e.Item.SubItems[1].Text == item.SubItems[0].Text)
                    {
                        listMailSend.Items[item.Index].Remove();
                    }
                }
            }
        }

        private void listEmployee_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (e.Item.Checked)
            {
                ListViewItem item = new ListViewItem(e.Item.SubItems[1].Text);
                item.SubItems.Add(e.Item.SubItems[2].Text);
                listMailSend.Items.Add(item);
            }
            else
            {
                foreach (ListViewItem item in listMailSend.Items)
                {
                    if (e.Item.SubItems[1].Text == item.SubItems[0].Text)
                    {
                        listMailSend.Items[item.Index].Remove();
                    }
                }
            }
        }

        private void bEmployee_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listEmployee.Items)
            {
                item.Checked = true;
            }
        }
        public Collection<string> GoEmailSend()
        {
            Collection<string> email = new Collection<string>();
            foreach (ListViewItem item in listMailSend.Items)
            {
                email.Add(item.SubItems[1].Text);
            }
            return email;
        }

        private void bUnselectAdministrator_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem itemB in listAdministrator.Items)
            {
                    itemB.Checked = false;
            }
        }

        private void bCancel_Click(object sender, EventArgs e)
        {
            listMailSend.Items.Clear();
            this.Close();
        }

        private void bAccept_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bUselectClient_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listClient.Items)
            {
                item.Checked = false;
            }
        }

        private void bUnselectEmployee_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listEmployee.Items)
            {
                item.Checked = false;
            }
        }
    }
}
